﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Abigail.Ui
{
    public partial class UiEditKey : Form
    {
        Controller c;
        bool isCreatingNew;

        // Constructor that displays a create new key window
        public UiEditKey(Controller givenC, string[] userInitials)
        {
            InitializeComponent();
            c = givenC;
            isCreatingNew = true;

            if (userInitials != null)
                comboBox_user.Items.AddRange(userInitials);
            txt_updated.Text = "genereras automatiskt";

            this.Show();
        }

        // Constructor that displays an existing key window
        public UiEditKey(Controller givenC, string[] userInitials, Model.Key key)
        {
            InitializeComponent();
            c = givenC;
            isCreatingNew = false;

            txt_name.Text = key.Name;
            txt_name.Enabled = false;

            if (userInitials != null)
                comboBox_user.Items.AddRange(userInitials);
            comboBox_user.SelectedItem = key.UserInitials;
            
            if (key.Updated == DateTime.MinValue)
                txt_updated.Text = "genereras automatiskt";
            else
                txt_updated.Text = key.Updated.ToShortDateString();
            this.Show();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if (isCreatingNew)
                c.SaveNewKey(txt_name.Text, comboBox_user.SelectedItem.ToString(), this);
            else
                c.UpdateKey(txt_name.Text, comboBox_user.SelectedItem.ToString(), this);
        }
    }
}
